package com.hhj.sell.controller;

import com.hhj.sell.pojo.ProductCategory;
import com.hhj.sell.pojo.ProductInfo;
import com.hhj.sell.service.CategoryService;
import com.hhj.sell.service.ProductInfoService;
import com.hhj.sell.utils.ResultVoUtil;
import com.hhj.sell.vo.ProductInfoVo;
import com.hhj.sell.vo.ProductVo;
import com.hhj.sell.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*买家商品*/
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/list")
    public ResultVo list() {
        /*1.查询在架商品详情*/
        List<ProductInfo> productInfoList = productInfoService.findUpAll();
        /*2.根据商品的类目查看商品的类目的相关信息*/
        List<Integer> categoryTypeList = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategotyTypeIn(categoryTypeList);
        /*3.封装数据*/
        List<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVo>  productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
                productVo.setProductInfoVoList(productInfoVoList);

            }
            productVoList.add(productVo);
        }

        return ResultVoUtil.seccess(productVoList);
    }
}
